import numpy as np
import pylab as pl

def hyst(x, th_lo, th_hi=None, initial = False):
    if th_hi is None:
        th_hi = th_lo + np.finfo(np.float32).eps
    hi = x >= th_hi
    lo_or_hi = (x <= th_lo) | hi
    ind = np.nonzero(lo_or_hi)[0] #return the indces of those non-zero elements in lo_or_hi
    if not ind.size: # prevent index error if ind is empty
        return np.zeros_like(x, dtype=bool) | initial
    cnt = np.cumsum(lo_or_hi) # from 0 to len(x)
    hst = np.where(cnt, hi[ind[cnt-1]], initial)
    return hst
x = np.linspace(-1,1,10)
y = np.sin(x)
low = -0.5
high = 0.5
hst = hyst(y,low,high)
print(y)
